package demo.virtuemart.category;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.events.internal.EventFiringMouse;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import demo.virtuemart.Util.ErrorUtil;
import demo.virtuemart.Util.MouseListener;
import demo.virtuemart.Util.TestUtil;

public class CheckProductDisplay  extends TestSuiteBase {
	String [] runmode=null;
	static int count = -1;
	static boolean pass=false;
	static boolean fail=false;
	static boolean skip=false;
	@BeforeTest
	public void checkTestSkip() throws Throwable 
	{
		if(!TestUtil.isTestCaseRunnable(suite_category_xls, this.getClass().getSimpleName()))
		{
			logs.debug("Log Message---- Skipping the test case" +this.getClass().getSimpleName()+"as the run mode set to no");
		skip=true;
			throw new SkipException("Skipping the test case" +this.getClass().getSimpleName()+"as the run mode set to no");
		}
		runmode = TestUtil.getDataSetRunModes(suite_category_xls, this.getClass().getSimpleName());
	}
	
	
	@Test
	public void checkProductDisplay()
{
count++;
if(!runmode[count].equalsIgnoreCase("Y"))
{	
skip=true;
throw new SkipException("Run mode for test set data set " +count + "is set to no");
}
logs.debug("Executing Adding Product Test");
openBrowser();
MouseListener listen = new MouseListener();
EventFiringMouse mouse = new EventFiringMouse(driver,listen);
driver.navigate().to(props.getProperty("testSiteName"));
driver.manage().window().maximize();
if(!checkElementPresence("virtueMart2Menu"))
{
	fail=true;
	return;
}

driver.findElement(By.xpath(OR.getProperty("virtueMart2Menu"))).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);

new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@href='/index.php/2012-01-13-09-33-20/virtuemart-category-layout']")));
Locatable locate = (Locatable)driver.findElement(By.xpath("//a[@href='/index.php/2012-01-13-09-33-20/virtuemart-category-layout']"));


Coordinates cordi = locate.getCoordinates();
mouse.mouseMove(cordi);
mouse.click(cordi);

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

if(!verifyTitle("Category Layout"))
{
	fail=true;
	return;
}

WebElement categoryproducts = driver.findElement(By.className("category-products"));
List<WebElement> img = categoryproducts.findElements(By.tagName("img"));
int imgsize = img.size();
System.out.println(imgsize);
if(!compareNumbers(imgsize, 10))
{
	fail=true;
	return;
}
WebElement productDetails = driver.findElement(By.className("product-link"));
List<WebElement> prddetails = productDetails.findElements(By.tagName("span"));
int prdsize = prddetails.size();
System.out.println(prdsize);
if(!compareNumbers(prdsize, 10))
{
	fail=true;
	return;
}
}
	@DataProvider
	public Object[][] getTestData() throws Throwable
	{
		return TestUtil.getData(suite_category_xls, this.getClass().getSimpleName());
	}
	@AfterMethod
	public void report() throws Throwable
	{
		if(skip)
		{
			TestUtil.reportDataSetResult(suite_cart_xls, this.getClass().getSimpleName(), count+2, "Skip");
		}
		else if(fail)
		{
			  TestUtil.reportDataSetResult(suite_cart_xls, this.getClass().getSimpleName(), count+2, "Fail");
		}
		else
		{
			  TestUtil.reportDataSetResult(suite_cart_xls, this.getClass().getSimpleName(), count+2, "Pass");
		}
		skip=false;
		fail=false;
		pass=false;
	}
	@AfterTest
	public void reportTestResult() throws Throwable
	{
		if(pass)
			TestUtil.reportDataSetResult(suite_category_xls, "Test Cases", TestUtil.getRowNum(xlsSuite, this.getClass().getSimpleName()), "Pass");
			
		else
			TestUtil.reportDataSetResult(suite_category_xls, "Test Cases", TestUtil.getRowNum(xlsSuite, this.getClass().getSimpleName()), "Pass");
	}
}
